<?php
/**
 * Created by PhpStorm.
 * User: yinian
 * Date: 2018/11/10
 * Time: 18:30
 */


//店铺类
class api{
    public $db;
    public $uid;
    public function __construct($db,$admin)
    {
        if(!admin_authority_check()) header("Location:".url('index-login'));
        $this->db = $db;
        $this->uid = $admin['uid'];
    }

    public function storeCate()
    {
        $finance = db_sql_find("SELECT id,name FROM ".$this->db->tablepre."cate where uid=$this->uid and status=1");
        json(['code'=>0,'msg'=>true,'count'=>0,'data'=>$finance]);
    }

    public function cateAdd()
    {
        $name = param('name');
        $uid = (int)param('uid',0);
        if ($uid == 0){
            $uid = $this->uid;
            $is_public = 1;
        }else{
            $is_public = 2;
        }
        $cate = db_insert('cate',array('name'=>$name,'uid'=>$uid,'status'=>1,'is_public'=>$is_public));
        if ($cate){
            json(array('data'=>'添加成功','status'=>true));
        }
        json(array('data'=>'添加失败','status'=>false));
    }

    public function goods()
    {
        $id = (int)param('id');$uid = (int)param('uid',0);if ($uid == 0)$uid = $this->uid;
        if (!is_int($id))json(['code' => 100, 'msg' => '请正确输入', 'count' => 0, 'data' => []]);
        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$uid and id=$id and status=1");
        if (!$cate)json(['code' => 100, 'msg' => '不存在的店铺', 'count' => 0, 'data' => []]);
        $finance = db_sql_find("SELECT id,cid,price_start,price_end FROM ".$this->db->tablepre."cate_price where cid=$id and status=1");
        json(['code'=>0,'msg'=>true,'count'=>0,'data'=>$finance]);
    }

    public function storeDel()
    {
        $id = (int)param('id');$uid = (int)param('uid',0);
        if ($uid == 0) $uid = $this->uid;
        if (!is_int($id))json(array('data'=>'请正确输入','status'=>false));
        $result = db_update("cate", array('uid'=>$uid,'id'=>$id),array('status'=>2));
        if ($result){
            json(array('data'=>'删除成功','status'=>true));
        }else{
            json(array('data'=>'删除失败','status'=>false));
        }
    }

    public function storeUpdate()
    {
        $id = (int)param('id');
        $uid = (int)param('uid',0);
        $name = param('name');
        $value = param('value');
        if (!is_int($id))json(array('data'=>'请正确输入','status'=>false));
        if ($uid == 0)$uid = $this->uid;
        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$uid and id=$id and status=1");
        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $result = db_update("cate", array('id'=>$id),array($name=>$value));
        if ($result){
            json(array('data'=>'修改成功','status'=>true));
        }else{
            json(array('data'=>'修改失败','status'=>false));
        }
    }

    public function priceAdd()
    {
        $price_start = param('price_start');$price_end = param('price_end');
        $cid = (int)param('cid');$uid = (int)param('uid',0);if ($uid == 0) $uid = $this->uid;
        if (!is_int($cid))json(array('data'=>'请正确输入','status'=>false));
        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$uid and id=$cid and status=1");
        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $cate = db_insert('cate_price',array('price_start'=>$price_start,'price_end'=>$price_end,'cid'=>$cid,'status'=>1));
        if ($cate){
            json(array('data'=>'添加成功','status'=>true));
        }
        json(array('data'=>'添加失败','status'=>false));
    }

    public function goodsUpdate()
    {
        $id = (int)param('id');
        $cid = (int)param('cid');
        $name = param('name');
        $value = param('value');
        if (!is_int($id) && !is_int($cid) )json(array('data'=>'请正确输入','status'=>false));
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
//        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $result = db_update("cate_price", array('id'=>$id,'cid'=>$cid),array($name=>$value));
        if ($result){
            json(array('data'=>'修改成功','status'=>true));
        }else{
            json(array('data'=>'修改失败','status'=>false));
        }
    }

    public function goodsDel()
    {
        $cid = (int)param('cid');
        $id = (int)param('id');
        if (!is_int($id) && !is_int($cid))json(array('data'=>'请正确输入','status'=>false));
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
//        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $result = db_update("cate_price", array('cid'=>$cid,'id'=>$id),array('status'=>2));
        if ($result){
            json(array('data'=>'删除成功','status'=>true));
        }else{
            json(array('data'=>'删除失败','status'=>false));
        }
    }

    public function goodsP()
    {
        $id = (int)param('id');$cid = param('cid');
        if (!is_int($id) && !is_int($cid))json(['code'=>100,'msg'=>'请正确输入','count'=>0,'data'=>[]]);
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
        $price = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate_price where cid=$cid and id=$id and status=1");
        if (!$price)json(['code'=>100,'msg'=>'不存在的店铺','count'=>0,'data'=>[]]);
        $finance = db_sql_find("SELECT id,cid,pid,`name` FROM ".$this->db->tablepre."cate_goods where pid=$id and cid=$cid and status=1");
        json(['code'=>0,'msg'=>true,'count'=>0,'data'=>$finance]);
    }

    public function goodsAdd()
    {
        $name = param('name');$cid = (int)param('cid');
        $pid = (int)param('pid');
        if (!is_int($cid) && !is_int($pid))json(array('data'=>'请正确输入','status'=>false));
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
//        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $cate = db_insert('cate_goods',array('name'=>$name,'pid'=>$pid,'cid'=>$cid,'status'=>1));
        if ($cate){
            json(array('data'=>'添加成功','status'=>true));
        }
        json(array('data'=>'添加失败','status'=>false));
    }

    public function goodsPUpdate()
    {
        $cid = (int)param('cid');$id = (int)param('id');$pid = (int)param('pid');$name = param('name');$value = param('value');
        if (!is_int($id) && !is_int($cid) && !is_int($pid))json(array('data'=>'请正确输入','status'=>false));
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
//        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $result = db_update("cate_goods", array('cid'=>$cid,'id'=>$id,'pid'=>$pid),array($name=>$value));
        if ($result){
            json(array('data'=>'修改成功','status'=>true));
        }else{
            json(array('data'=>'修改失败','status'=>false));
        }
    }

    public function goodsPDel()
    {
        $cid = (int)param('cid');$id = (int)param('id');$pid = (int)param('pid');
        if (!is_int($id) && !is_int($cid) && !is_int($pid))json(array('data'=>'请正确输入','status'=>false));
//        $cate = db_sql_find("SELECT id FROM ".$this->db->tablepre."cate where uid=$this->uid and id=$cid and status=1");
//        if (!$cate)json(array('data'=>'不存在的店铺','status'=>false));
        $result = db_update("cate_goods", array('cid'=>$cid,'id'=>$id,'pid'=>$pid),array('status'=>2));
        if ($result){
            json(array('data'=>'删除成功','status'=>true));
        }else{
            json(array('data'=>'删除失败','status'=>false));
        }
    }

    public function recharge()
    {
        $id = (int)param('id'); $recharge = param('recharge');
        if (!is_int($id))json(array('data'=>'请正确输入','status'=>false));
        $coin = db_find_one('user',array('uid'=>$id));
        if (strcmp('-',substr($recharge,0,1)) === 0){
            $recharge = substr($recharge,1,strlen($recharge)-1);
            if (!is_numeric($recharge)){
                json(array('data'=>'不是数字','status'=>false));
            }
            $recharge = $coin['coin'] - $recharge;
        }else{
            if (!is_numeric($recharge)){
                json(array('data'=>'不是数字','status'=>false));
            }
            $recharge = $coin['coin'] + $recharge;
        }
        $result = db_update('user',array('uid'=>$id),array('coin'=>$recharge));
        if ($result){
            json(array('data'=>'充值成功','status'=>true));
        }
        json(array('data'=>'充值失败','status'=>false));
    }

    public function shops()
    {
        $user = db_sql_find("SELECT uid,username,email FROM ".$this->db->tablepre."user where is_admin<>1 ");
        $count = db_sql_find("SELECT count(uid) as a FROM ".$this->db->tablepre."user where is_admin<>1 ");
        json(['code'=>0,'msg'=>true,'count'=>$count[0]['a'],'data'=>$user]);
    }

    public function shopsGoods()
    {
        $id = (int)param('id');
        if (!is_int($id))json(array('data'=>'请正确输入','status'=>false));
        db_sql_find("SELECT  FROM ".$this->db->tablepre."cate where is_admin<>1 ");
    }

    public function cate()
    {
        $id = (int)param('uid');
        if (!is_int($id))json(array('data'=>'请正确输入','status'=>false));
        $finance = db_sql_find("SELECT id,uid,name FROM ".$this->db->tablepre."cate where uid=$id and status=1");
        json(['code'=>0,'msg'=>true,'count'=>0,'data'=>$finance]);
    }

    public function __call($name, $arguments){
        echo '没有此方法';exit;

    }
}
$action = param(1);
$api = new api($db,$admin);
$api->$action();